﻿namespace PreGUI.Widgets
{
    using PreGUI.Interfaces;
    using UnityEngine;
    using UnityEngine.UI;
    using PreGUI.Helpers;

    /// <summary>
    /// PreGui Horizontal Layout Control
    /// </summary>
    public class PGHorizontalLayout : IPGControl
    {
        private IPGControl[] _children;

        public GameObject LayoutControls(PreGui gui, GameObject parent, string windowName)
        {
            var gameObject = new GameObject("PGVerticalLayout");
            gameObject.transform.SetParent(parent.transform, false);

            var layout = gameObject.AddComponent<HorizontalLayoutGroup>();
            layout.childForceExpandHeight = false;
            layout.padding = new RectOffset(6, 6, 6, 6);
            layout.spacing = 2f;

            _children.ProcessControls(gui, gameObject, windowName);

            return gameObject;
        }


        public IPGControl GenerateFromXmlNode(System.Xml.XmlNode node)
        {
            _children = new IPGControl[node.ChildNodes.Count];
            for(var i = 0; i <node.ChildNodes.Count; i++)
            {
                _children[i] = node.ChildNodes.Item(i).XmlProcessChildControl();
            }

            return this;
        }
    }
}

